package com.sdkx.app.ViewModel;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sdkx.app.model.AirWay;
import com.sdkx.app.model.TrackLine;
import com.sdkx.app.model.TrackPoint;
import com.sdkx.app.model.WayPoint;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据库帮助类,含测试部分
 */
public class DBHelper {
    private static final String TAG = DBHelper.class.getSimpleName();

    //获取数据库
    public static SQLiteDatabase getDatabase(){
        return LitePal.getDatabase();
    }

    //创建航点
    public static void createSampleData_waypoint(){
//        for(int i=0;i<10;i++){
//            WayPoint wayPoint = new WayPoint();
//            wayPoint.setName("test"+i);
//            wayPoint.setLongitude(123.0d+i*1.5);
//            wayPoint.setLatitude(123.0d+i*1.5);
//            wayPoint.setCreateTime(new Date());
//            wayPoint.setIsShow(i%2==0?true:false);
//            wayPoint.setType(WayPoint.WayPointTypes.AIS_AIRPORT.ordinal());
//            wayPoint.setIcon("");
//            if(!WayPointManager.insert(wayPoint)){
//                Log.e(TAG,"save waypoint fail,name test"+i);
//            }
//        }
    }
    //创建航线
    public static void createSampleData_airway(){
        for(int i=0;i<10;i++){
            AirWay airWay = new AirWay();
            airWay.setName("way"+i);
            airWay.setType(AirWay.TYPE_CUSTOM);
            airWay.setIsEdit(true);
            airWay.setIsShow(true);
            if(!AirWayManager.insert(airWay)){
                Log.e(TAG,"save airway fail,name way"+i);
            }
        }
    }
    //将航点与航线建立联系
    public static void createSampleData_airwaySegment(){
        AirWay airWay = AirWayManager.getItem("way1");
        List<WayPoint> wayPoints = WayPointManager.getItems(null);
        List<WayPoint> addWayPoints = wayPoints.subList(0,3);
        AirWayManager.updateWayPointsInAirWay(airWay.getName(),addWayPoints);
    }
    //获取航线的点测试
    public static void testGetPointsInWay() {
        List<WayPoint> wayPoints = AirWayManager.getWayPointsInAirWay("way1");
        if (wayPoints.size() == 3)
            Log.d(TAG, "getPointsInWay success!");
        else
            Log.d(TAG, "getPointsInWay fail!");
    }

    //更新航点信息测试
    public static void testUpdateWayPoint() {
        WayPoint wayPoint = WayPointManager.getItem("test1");
        int lastId = wayPoint.getId();
        wayPoint.setName("now_name");
        WayPointManager.update(wayPoint);
        wayPoint = WayPointManager.getItem("now_name");
        int nowId = wayPoint.getId();
        if (lastId == nowId)
            Log.d(TAG, "update name success!");
        else
            Log.d(TAG, "update name fail!");
    }


    public static void testGetId() {
        AirWay airWay = new AirWay();
        int lastId = airWay.getId();
        airWay = AirWayManager.getItem("way2");
        int nowId = airWay.getId();
        if (nowId != lastId)
            Log.d(TAG, "getId success!");
        else
            Log.d(TAG, "getId fail!");

    }

    //航迹相关测试
    public static void createSamepleDate_trackLine(){
       // 创建三条航迹线
        List<TrackLine> trackLines = new ArrayList<>();
        for(int i=0;i<3;i++) {
            TrackLine trackLine = new TrackLine();
            trackLine.setName("test_track");
            TrackLineManager.insert(trackLine);
            trackLines.add(trackLine);
        }

        //给航迹线1添加100个航迹点
        for(int i=0;i<100;i++){
            TrackPoint trackPoint = new TrackPoint();
            trackPoint.setCreateTime(System.currentTimeMillis());
//            trackPoint.setLatitude(123.0+i);
//            trackPoint.setLongitude(100.0+i);
            trackPoint.setAltitude(100+i*100);
            trackPoint.setAngle(3.6*i);
            trackPoint.setSpeed(300);
            trackPoint.setTrackLine(trackLines.get(0).getCreateTime());
            TrackPointManager.insert(trackPoint);
        }
    }



}
